import axios from "axios";
import { store } from "@/store/store";
import { message } from "antd";
import { loginout } from "@/store/actions/login";
import {customHistory} from './history'
export const requst = axios.create({
  baseURL:'http://geek.itheima.net/v1_0'
})
requst.interceptors.request.use(function (config) {
  // 在发送请求之前做些什么
  const token = store.getState().login
  if (!config.url.startsWith('/authorizations')) {
    config.headers.Authorization = `Bearer ${token}`
  }
  return config;
}, function (error) {
  // 对请求错误做些什么
  return Promise.reject(error);
});
//响应拦截器
requst.interceptors.response.use(undefined, (error) => {
  if (!error.response) {
    message.error('网络请求超时')
    return Promise.reject(error);
  }
  if (error.response.status === 401) {
    message.error(error.response.data.message, 1.5, () => {
      store.dispatch(loginout())
      customHistory.push('/login', {
        from:customHistory.location.pathname
      })
    })
    return Promise.reject(error);
  }
});